﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.Web;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Enterprise.RelationQuery
{
    public partial class DistributionRelationToQuery : BasePage
    {
        public DistributionRelationToQuery()
        {
            this.ModuleNo = Module.分配送管理.ToString();
        }

        IBLL_Distribution iDistribution = Factory.BLL_Distribution();
        IBLL_SelectRelation iselectRelation = Factory.BLL_SelectRelation();
        IBLL_Company IBC = Factory.BLL_Company();

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                if (CurrentAccount.GetUser().UserRole.RoleID == "05")
                {

                    lblNav.Text = "<ul><li><a href=\"CompanyMaintain.aspx\">配送企业管理</a></li><li><a href=\"RelationQueryUnpack.aspx\">配送产品管理</a></li><li class=\"active\"><a href=\"javascript:void(0);\">配送关系查询</a></li></ul>";

                    BangdinGvRelaion();
                }
                else
                {
                    HZ.DrugTrade.Model.Query.Company com = new HZ.DrugTrade.Model.Query.Company();
                    com.CompanyID = CurrentAccount.GetUser().Org.OrgID; ;
                    int outINt = 0;
                    IList<Company> lst = IBC.GetCompanyByWhere(com, 1, 1, out outINt);

                    if (lst != null && lst[0].CompanyType == EnumCompanyType.配送企业)
                    {
                        this.btnQuery0.Enabled = false;
                    }
                    else
                    {
                        lblNav.Text = "<ul><li><a href=\"CompanyMaintain.aspx\">配送企业管理</a></li><li><a href=\"RelationQueryUnpack.aspx\">配送产品管理</a></li><li class=\"active\"><a href=\"javascript:void(0);\">配送关系查询</a></li><li ><a href=\"DistributionRelationToConfirm.aspx\">配送关系确认</a></li></ul>";

                        BangdinGvRelaion();
                    }

                }
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            BangdinGvRelaion();
        }
        /// <summary>
        /// 绑定事件
        /// </summary>
        private void BangdinGvRelaion()
        {
            string strWhere = GetCondition();

            strWhere += " and companyid_tb='" + CurrentAccount.GetUser().Org.OrgID + "' order by procurecatalogId,areaid,companyid_PS ";

            int totalCount = 0;
            AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE;
            DataTable dt = iselectRelation.GetSelectRelationToConfirm(strWhere, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
            GvRelaion.DataSource = dt;
            GvRelaion.DataBind();
            AspNetPager1.RecordCount = totalCount;
        }

        protected string GetCondition()
        {
            string strWhere = " and ISUsing=1  ";

            #region 查询条件
            if (!string.IsNullOrEmpty(HZ.Fun2.CheckString(this.txtGoodsName.Text.Trim())))
            {
                strWhere += "  and GoodsName like '%" + HZ.Fun2.CheckString(this.txtGoodsName.Text.Trim()) + "%'";
            }
            //if (!string.IsNullOrEmpty(HZ.Fun2.CheckString(this.txtPinyinCode.Text.Trim())))
            //{
            //    strWhere += " and PinyinCode like '%" + HZ.Fun2.CheckString(this.txtPinyinCode.Text.Trim()) + "%'";
            //}
            if (!string.IsNullOrEmpty(HZ.Fun2.CheckString(this.txtOutlookc.Text.Trim())))
            {
                strWhere += " and Outlookc like '%" + HZ.Fun2.CheckString(this.txtOutlookc.Text.Trim()) + "%'";
            }
            if (!string.IsNullOrEmpty(HZ.Fun2.CheckString(this.txtUnit.Text.Trim())))
            {
                strWhere += " and Unit like '%" + HZ.Fun2.CheckString(this.txtUnit.Text.Trim()) + "%'";
            }
            if (!string.IsNullOrEmpty(HZ.Fun2.CheckString(txtMaterial.Text.Trim()))) //材质
            {
                strWhere += " and Material like '%" + HZ.Fun2.CheckString(txtMaterial.Text.Trim()) + "%'";
            }
            if (!string.IsNullOrEmpty(HZ.Fun2.CheckString(txtCompanyName_ps.Text.Trim()))) //配送企业
            {
                strWhere += " and Companyname_ps like '%" + HZ.Fun2.CheckString(txtCompanyName_ps.Text.Trim()) + "%'";
            }
            if (!string.IsNullOrEmpty(HZ.Fun2.CheckString(this.txtpinyincode.Text.Trim()))) //拼音码
            {
                strWhere += " and UPPER(PinyinCode) like '%'||UPPER('" + HZ.Fun2.CheckString(txtpinyincode.Text.Trim()) + "')||'%'";
            }
            strWhere += " and ProjectID='" + this.ddlProjectid.SelectedValue + "'";
            if (string.IsNullOrEmpty(this.DllStatusType.SelectedValue))
            {

            }
            else
            {
                strWhere += "  and status='" + DllStatusType.SelectedItem.Text + "' ";
            }
            if (string.IsNullOrEmpty(this.DllArea.SelectedValue))
            {
            }
            else
            {
                strWhere += "  and AreaName='" + DllArea.SelectedItem.Text + "' ";
            }
            #endregion

            return strWhere;
        }

        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            BangdinGvRelaion();
        }

        /// <summary>
        /// 将给定的url加上script 实现页面跳转
        /// 针对button 的 onclintclick 新窗口打开
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        protected string OpenURLScript(string str)
        {
            int index = str.IndexOf("?");
            //给url加returnUrl,值为当前页面的Url
            if (index != -1)
            {
                str += "&" + Constants.QUERYSTRING_RETURN_URL + "=" + Page.Request.RawUrl;
            }
            else
            {
                str += "?" + Constants.QUERYSTRING_RETURN_URL + "=" + Page.Request.RawUrl;
            }
            string strScript = "if(!this.disabled){window.open('" + str + "');} return false;";
            return strScript;
        }


        protected void btnExport_Click(object sender, EventArgs e)
        {

            string strWhere = GetCondition();

            strWhere += " and companyid_tb='" + CurrentAccount.GetUser().Org.OrgID + "' order by productname ";

            int totalCount = 0;

            DataTable dt = iselectRelation.GetSelectRelationToConfirm(strWhere, 1, 0, out totalCount);

            HZ.Excel.ExcelHelper.ExportDataForWeb(dt, GvRelaion);
        }
    }
}
